package com.example.springboot.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.example.springboot.controller.request.BaseRequest;
import com.example.springboot.entity.Appointment;
import com.example.springboot.exception.ServiceException;
import com.example.springboot.mapper.AppointmentMapper;
import com.example.springboot.service.IAppointmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class AppointmentService implements IAppointmentService {

    @Autowired //通过autowired导入appointmentMapper
    AppointmentMapper appointmentMapper;

    public List<Appointment> list(){
        return appointmentMapper.list();
    }

    @Override
    public PageInfo<Appointment> page(BaseRequest baseRequest){
//        dependency引入pagehelper
//        PageHelper传入页面和每页信息条数
        PageHelper.startPage(baseRequest.getPageNum(),baseRequest.getPageSize());
        List<Appointment> appointments=appointmentMapper.listByCondition(baseRequest);
        return new PageInfo<>(appointments);
    }

    @Override
    public void save(Appointment appointment){//        当做卡号来处理
        Date date= new Date();
        try{
            appointmentMapper.save(appointment);
        }catch(DuplicateKeyException e){
            log.error("数据插入失败,name:{}",appointment.getUserName());
            throw new ServiceException("用户插入失败");
        }
    }

    @Override
    public Appointment getById(Integer id) {
        return appointmentMapper.getById(id);
    }

    @Override
    public void update(Appointment appointment) {
        appointment.setUpdateTime(new Date());
        appointmentMapper.updateById(appointment);

    }

    @Override
    public void deleteById(Integer id) {
        appointmentMapper.deleteById(id);
    }
    
}
